<?php

namespace app\api\controller;


use app\api\BaseController;
use app\common\model\Member;
use think\Request;
use think\Db;

/**
 * @title 优惠券
 *
 */
class Coupon extends BaseController
{

    /**
     * Notes: 优惠券列表
     * User: shen
     * Date: 2020/4/22
     * Time: 16:37
     */
    public function coupon_list()
    {

        $where['member_id'] = $this->user_id;
        $where['is_used'] = 1;
        $invoice_info = db('voucher_collection')->where($where)->field('voucher_id')->select();
        if ($invoice_info) {
            $ids = implode(',', array_column($invoice_info, 'voucher_id'));
            $wheres['voucher_id'] = array('not in', $ids);
        } else {
            $wheres = [];
        }

        $wheres['voucher_enddate'] = array('>', time());
        $wheres['voucher_startdate'] = array('<', time());
        $wheres['count'] = array('>', 0);
        $wheres['is_del'] = array('eq', 0);
        $wheres['status'] = array('eq', 1);
        // p($wheres);die;
        $voucher_info = db('voucher')->where($wheres)->select();
        return json_success_return($voucher_info);
    }

    /**
     * Notes:首页弹优惠券列表
     * User: shen
     * Date: 2020/5/8
     * Time: 16:43
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
     public function app_index_coupon_list()
    {
        $where['member_id'] = $this->user_id;
        $where['is_used'] = 1;
        $invoice_info = db('voucher_collection')->where($where)->field('voucher_id')->select();
        if ($invoice_info) {
            $ids = implode(',', array_column($invoice_info, 'voucher_id'));
            $wheres['t.voucher_id'] = array('not in', $ids);
        } else {
            $wheres = [];
        }

        #先查注册的
        $wheres['v.voucher_enddate'] = array('>', time());
        $wheres['v.voucher_startdate'] = array('<', time());
        $wheres['v.count'] = array('>', 0);
        $wheres['v.is_del'] = array('eq', 0);
        $wheres['v.status'] = array('eq', 1);
        $wheres['t.type_status'] = array('eq', 1);
        $wheres['t.is_del'] = array('eq', 0);
        $wheres['t.pattern_id'] = array('eq', '1');
        // p($wheres);die;
        $voucher_info = db('voucher_type')->alias('t')->join('voucher v','t.voucher_id = v.voucher_id')->where($wheres)->select();

        #再查定时发放的
        $wheres1['v.voucher_enddate'] = array('>', time());
        $wheres1['v.voucher_startdate'] = array('<', time());
        $wheres1['v.count'] = array('>', 0);
        $wheres1['v.is_del'] = array('eq', 0);
        $wheres1['v.status'] = array('eq', 1);
        $wheres1['t.type_status'] = array('eq', 1);
        $wheres1['t.is_del'] = array('eq', 0);
        $wheres1['t.pattern_id'] = array('eq', '2');
        // p($wheres);die;
        $voucher_info1 = db('voucher_type')->alias('t') ->whereTime('t.timing_start_time', '<', date("Y-m-d H:i",time())) ->whereTime('t.timing_end_time', '>', date("Y-m-d H:i",time()))->join('voucher v','t.voucher_id = v.voucher_id')->where($wheres1)->select();

       p($voucher_info1);die;

        return json_success_return($voucher_info);

    }

}


?>





